﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace MusicWorks.Web.Logic.Helpers
{
    public class CookieSessionValueProvider : ISessionValueProvider
    {
        const string SessionCookieName = "MusicWorks_Session";
        const string UserIdCookieValueName = "UserId";
        public Guid UserId
        {
            get
            {
                return GetCookieGuidValue(SessionCookieName);
            }
            set
            {
                SetCookie(SessionCookieName, value.ToString());
            }
        }

        static Guid GetCookieGuidValue(string name)
        {
            var value = GetCookieValue(name);
            return value == null ? Guid.Empty : new Guid(value);
        }

        static int GetCookieIntValue(string name)
        {
            return GetCookieValue(name, 0, v => { int value; if (int.TryParse(v, out value)) return value; return 0; });
        }

        static decimal GetCookieDecimalValue(string name)
        {
            return GetCookieValue(name, 0M, v => { decimal value; if (decimal.TryParse(v, out value)) return value; return 0M; });
        }

        static T GetCookieValue<T>(string name, T defaultValue, Func<string, T> conversion)
        {
            var value = GetCookieValue(name);
            return value == null ? defaultValue : conversion(value);
        }

        static string GetCookieValue(string name)
        {
            var cookie = HttpContext.Current.Request.Cookies[SessionCookieName];
            if (cookie == null || string.IsNullOrEmpty(cookie.Values[name])) return null;
            return cookie.Values[name];
        }

        static void SetCookie(string name, string value)
        {
            var cookie = HttpContext.Current.Request.Cookies[SessionCookieName];
            if (cookie == null)
            {
                cookie = new HttpCookie(SessionCookieName);
                cookie.Values.Add(name, value);
                HttpContext.Current.Request.Cookies.Add(cookie);
                HttpContext.Current.Response.AppendCookie(cookie);
            }
            else
            {
                cookie.Value = value;
                HttpContext.Current.Request.Cookies.Set(cookie);
                HttpContext.Current.Response.SetCookie(cookie);
            }
        }

        const string ShoppingBasketIdCookieValueName = "ShoppingBasketId";
        public Guid BasketId
        {
            get
            {
                return GetCookieGuidValue(ShoppingBasketIdCookieValueName);
            }
            set
            {
                SetCookie(ShoppingBasketIdCookieValueName, value.ToString());
            }
        }

        const string ShoppingBasketValueCookieValueName = "ShoppingBasketValue";
        public decimal BasketValue
        {
            get
            {
                return GetCookieDecimalValue(ShoppingBasketValueCookieValueName);
            }
            set
            {
                SetCookie(ShoppingBasketValueCookieValueName, value.ToString());
            }
        }

        const string ShoppingBasketItemCountCookieValueName = "ShoppingBasketItemCount";
        public int ItemsInBasket
        {
            get
            {
                return GetCookieIntValue(ShoppingBasketItemCountCookieValueName);
            }
            set
            {
                SetCookie(ShoppingBasketItemCountCookieValueName, value.ToString());
            }
        }
    }
}
